package com_.dxy_test_.redis_;

import io.lettuce.core.RedisClient;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.async.RedisAsyncCommands;
import io.lettuce.core.api.sync.RedisCommands;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import redis.clients.jedis.Jedis;

/**
 * @Description TODO
 * @Author dangxianyue
 * @Date 2022/11/8 18:23
 */
public class RedisConfig {

    private static final String HOST = "172.25.20.87";

    private static final int PORT = 6379;

    //redis://[password@]host:port[/databaseNumber], 其中中括号是可选的，也就是如果没有可以省略
    private static final String REDIS_URI = "redis://" + HOST + ":" + PORT;

    /**
     * jedis客户端
     */
    public static Jedis jedis;

    /**
     * lettuce客户端（对redis的操作是同步的）
     */
    public static RedisCommands<String, String> syncCommands;

    /**
     * lettuce客户端（对redis的操作是异步的）
     */
    public static RedisAsyncCommands<String, String> asyncCommands;

    /**
     * redisson客户端
     */
    public static RedissonClient redissonClient;

    static {

        initJedis();

        initLettuce();

        initRedissonClient();
    }

    private static void initRedissonClient() {
        Config config = new Config();
        SingleServerConfig server = config.useSingleServer();
        //锁没有设置释放时间，会启动看门狗线程，默认是30秒过期，10秒进行一次续命检查，以处设置为5秒过期
        //config.setLockWatchdogTimeout(5 * 1000L);
        server.setAddress(REDIS_URI);
        //server.setPassword(password);//redis没有密码就不用设置
        redissonClient = Redisson.create(config);
    }

    private static void initLettuce() {
        //连接redis服务端
        //连接格式：redis://[password@]host:port[/databaseNumber]  其中中括号是可选的，也就是如果没有可以省略
        RedisClient redisClient = RedisClient.create(REDIS_URI);

        StatefulRedisConnection<String, String> connection = redisClient.connect();
        //对redis的操作是同步的
        syncCommands = connection.sync();
        //对redis的操作是异步的
        asyncCommands = connection.async();
    }

    private static void initJedis() {
        jedis = new Jedis(HOST,PORT);
    }

}
